package edu.ptit.app;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ptit.jad.configuration.CONSTANTS;
import edu.ptit.jad.data.DataAccess;
import edu.ptit.jad.data.MatrixUtils;
import edu.ptit.jad.model.ContentSimilarity;
import edu.ptit.jad.similarity.Score;
import edu.ptit.jad.similarity.ScoreType;

public class AppQuery {

	// score
	static double[][] tfidf_simi = null;
	static double[][] lda_simi = null;
	static double[][] at_lda_simi = null;
	static double[][] at_cate_lda_simi = null;

	private static Map<Integer, String> ads = new HashMap<Integer, String>();
	private static Map<Integer, String> news = new HashMap<Integer, String>();

	public static void init() throws Exception {

		tfidf_simi = MatrixUtils.readMatrix("data/output/tf_idf.simi.jad");
		lda_simi = MatrixUtils.readMatrix("data/output/lda.simi.jad");
		at_lda_simi = MatrixUtils.readMatrix("data/output/at_lda.simi.jad");
		at_cate_lda_simi = MatrixUtils
				.readMatrix("data/output/at_cats_lda.simi.jad");

		processFileDir("data/test/100_test_news.jad", news);
		processFileDir("data/test/2000_test_ads.jad", ads);

	}

	public static void main(String[] args) throws Exception {

		init();

		List<String> estimates = DataAccess.getInstance().getContents(
				"data/estimates/estimates.jad");

		// File file = new File("data/estimates/human_estimates.jad");
		// BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
		// new FileOutputStream(file), "UTF8"));

		int numberCorrect1th = 0;
		int numberCorrect2th = 0;
		int numberCorrect3th = 0;
		int numberCorrect4th = 0;

		int correctInTop = 0;

		for (int i = 0; i < news.size(); i++)

		{
			int docId = i + 1;

			// ScoreType type = ScoreType.TFIDF_AND_LDA;
			// /Score score = new Score(ScoreType.TFIDF_AND_LDA);

			// System.err.println(news.get(docId));

			// out.write("[docid:" + docId + "][" + news.get(docId) + "]");
			// out.write("\n");

			List<ContentSimilarity> contentSimilaritys = new ArrayList<ContentSimilarity>();

			//query(docId, ScoreType.TF_IDF, contentSimilaritys);
			// query(docId, ScoreType.TFIDF_AND_LDA, contentSimilaritys);
			 query(docId, ScoreType.TFIDF_AND_AT, contentSimilaritys);
			// query(docId, ScoreType.TFIDF_AND_AT_LDA, contentSimilaritys);

			ContentSimilarity[] sis = ContentSimilarity.getTopN(
					contentSimilaritys, 4);
			//
			// for (ContentSimilarity si : sis) {
			//
			// out.write("\t" + si.getId() + "[" + si.getSimilarity() + "]"
			// + "[" + ads.get(si.getId()) + "]");
			// out.write("\n");
			//
			// }
			//
			// contentSimilaritys = new ArrayList<ContentSimilarity>();
			// query(docId, ScoreType.TFIDF_AND_LDA, contentSimilaritys);
			// sis = ContentSimilarity.getTopN(contentSimilaritys, 4);
			//
			// for (ContentSimilarity si : sis) {
			//
			// out.write("\t" + si.getId() + "[" + si.getSimilarity() + "]"
			// + "[" + ads.get(si.getId()) + "]");
			// out.write("\n");
			//
			// }
			//
			// contentSimilaritys = new ArrayList<ContentSimilarity>();
			// query(docId, ScoreType.TFIDF_AND_AT, contentSimilaritys);
			// sis = ContentSimilarity.getTopN(contentSimilaritys, 4);
			//
			// for (ContentSimilarity si : sis) {
			//
			// out.write("\t" + si.getId() + "[" + si.getSimilarity() + "]"
			// + "[" + ads.get(si.getId()) + "]");
			// out.write("\n");
			//
			// }
			//
			// contentSimilaritys = new ArrayList<ContentSimilarity>();
			// query(docId, ScoreType.TFIDF_AND_AT_LDA, contentSimilaritys);
			// sis = ContentSimilarity.getTopN(contentSimilaritys, 4);
			//
			// for (ContentSimilarity si : sis) {
			//
			// out.write("\t" + si.getId() + "[" + si.getSimilarity() + "]"
			// + "[" + ads.get(si.getId()) + "]");
			// out.write("\n");
			//
			// }

			// for (ContentSimilarity si : sis) {

			// System.out.print(si.getId() + "[" + si.getSimilarity() + "]"
			// + "[" + ads.get(si.getId()) + "]");
			// System.out.println("");

			// }
			try{

			String[] corrects = estimates.get(i).trim().split(" ");

			if (sis[0].getId() == Integer.parseInt(corrects[0]))
				numberCorrect1th++;
			if (sis[1].getId() == Integer.parseInt(corrects[1]))
				numberCorrect2th++;
			if (sis[2].getId() == Integer.parseInt(corrects[2]))
				numberCorrect3th++;
			if (sis[3].getId() == Integer.parseInt(corrects[3]))
				numberCorrect4th++;

			for (int k = 0; k < 4; k++) {

				for (int h = 0; h < 4; h++) {
					if (sis[k].getId() == Integer.parseInt(corrects[h])) {
						correctInTop++;

						break;
					}

				}
			}

			}catch (Exception e) {
				//e.printStackTrace();
				
				System.err.println(estimates.get(i));
			}
			// for (ContentSimilarity si : sis) {

			// System.out.print(si.getId() + "[" + si.getSimilarity() + "]"
			// + "[" + ads.get(si.getId()) + "]");
			// System.out.println("");

			// }

		}

		System.out.println(numberCorrect1th + " " + numberCorrect2th + " "
				+ numberCorrect3th + " " + numberCorrect4th);

		System.err.println("correct in top : " + correctInTop);

		// out.close();

	}

	public static void query(int docId, ScoreType type,
			List<ContentSimilarity> contentSimilaritys) {

		Score score = new Score(type);
		double tfidf;
		double lda;
		double au_lda;
		double au_cates_lda;

		for (int i = 0; i < ads.size(); i++) {

			tfidf = tfidf_simi[docId - 1][i];
			lda = lda_simi[docId - 1][i];
			au_lda = at_lda_simi[docId - 1][i];
			au_cates_lda = at_cate_lda_simi[docId - 1][i];

			contentSimilaritys.add(new ContentSimilarity(i + 1, score
					.calculate(tfidf, lda, au_lda, au_cates_lda)));
		}

	}

	public static void processFileDir(String fileDir, Map<Integer, String> map) {

		// Read read file

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileDir), "UTF-8"));
			String line;

			int index = 1;
			while ((line = reader.readLine()) != null) {

				if (line.equals(""))
					continue;

				map.put(index, line);

				index++;

			}

			reader.close();

		} catch (Exception e) {
			System.out.println("Error while reading dictionary:"
					+ e.getMessage());
			e.printStackTrace();

		}

	}

}
